package pers.sen.health.controller;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import pers.sen.health.entity.Result;
import pers.sen.health.redis.RedisUtil;
import pers.sen.health.resources.RedisMessageConstant;
import pers.sen.health.service.OrderService;
import pers.sen.health.sms.ValidateCodeUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/11/12 19:48
 * @Description TODO
 */
@RestController
public class OrderController {
    @Reference
    private OrderService orderService;

    @GetMapping("order/sendValidateCode")
    public Result findSetMealById(@RequestParam("telephone") String telephone){
        System.out.println("telephone = " + telephone);
        // 工具类生成验证码
        String code = ValidateCodeUtils.generateValidateCode(4);
        System.out.println("code = " + code);
        // 发送短信
        // SmsUtils.validUserTelephone(telephone,code);
        // 优化发送短信


        // 存储到Redis中60秒有效
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER+telephone,code,60, TimeUnit.SECONDS);


        // 将验证码发送到前端，模拟发短信接收
        return new Result(code);
    }


    @PostMapping("order/insertOrder")
    public Result insertOrder(@RequestBody Map map) {
        return new Result(orderService.insertOrder(map));
    }

}
